(function(){
  'use strict';

  if (!window.Auth) return;

  var segBtns = document.querySelectorAll('.seg-btn');
  var loginForm = document.getElementById('login-form');
  var registerForm = document.getElementById('register-form');
  var logoutBtn = document.getElementById('logout-btn');

  // Segmented tabs (login/register)
  for (var i=0;i<segBtns.length;i++){
    segBtns[i].addEventListener('click', function(e){
      var tab = e.currentTarget.getAttribute('data-tab');
      for (var j=0;j<segBtns.length;j++){ segBtns[j].classList.toggle('is-active', segBtns[j].getAttribute('data-tab') === tab); }
      var lf=document.getElementById('login-form'); var rf=document.getElementById('register-form');
      if (lf) lf.classList.toggle('is-hidden', tab !== 'login');
      if (rf) rf.classList.toggle('is-hidden', tab !== 'register');
    });
  }

  // Prevent legacy handlers; capture phase to intercept
  if (loginForm && !loginForm.getAttribute('data-bound')){
    loginForm.setAttribute('data-bound','1');
    loginForm.addEventListener('submit', function(e){
      e.preventDefault(); e.stopPropagation();
      var u = document.getElementById('login-username').value.trim();
      var p = document.getElementById('login-password').value.trim();
      var msg = document.getElementById('login-msg'); if (msg) msg.textContent='登录中...';
      window.Auth.login(u, p)
        .then(function(){ if (msg) msg.textContent='登录成功'; window.Auth.updateAuthView(); if (typeof window.loadSettingsData === 'function') window.loadSettingsData(); window.location.reload(); })
        .catch(function(err){ if (msg) msg.textContent = '登录失败：' + (err && err.message ? err.message : '未知错误'); });
    });
  }

  if (registerForm && !registerForm.getAttribute('data-bound')){
    registerForm.setAttribute('data-bound','1');
    registerForm.addEventListener('submit', function(e){
      e.preventDefault(); e.stopPropagation();
      var u = document.getElementById('reg-username').value.trim();
      var p = document.getElementById('reg-password').value.trim();
      var msg = document.getElementById('register-msg'); if (msg) msg.textContent='注册中...';
      fetch((window.API_BASE || 'http://43.153.150.144:8080') + '/auth/register', { method:'POST', headers:{'Content-Type':'application/json'}, body: JSON.stringify({ email:u, password:p, role:'user' }) })
        .then(function(res){ if (!res.ok) throw new Error('HTTP '+res.status); return res.json(); })
        .then(function(){ if (msg) msg.textContent='注册成功，请登录'; for (var j=0;j<segBtns.length;j++){ segBtns[j].classList.toggle('is-active', segBtns[j].getAttribute('data-tab') === 'login'); } if (document.getElementById('login-form')) document.getElementById('login-form').classList.remove('is-hidden'); if (document.getElementById('register-form')) document.getElementById('register-form').classList.add('is-hidden'); })
        .catch(function(err){ if (msg) msg.textContent = '注册失败：' + (err && err.message ? err.message : '未知错误'); });
    });
  }

  if (logoutBtn && !logoutBtn.getAttribute('data-bound')){
    logoutBtn.setAttribute('data-bound','1');
    logoutBtn.addEventListener('click', function(e){
      e.preventDefault(); e.stopPropagation();
      window.Auth.logout();
      window.Auth.updateAuthView();
    });
  }
})();


